package com.xdtech.sys.init;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Operation;

public abstract class SysAbstractInit implements SysInitOperation{
	protected Log log = LogFactory.getLog(getClass());
	protected BaseService<BaseModel> baseService;
	protected void initButtonCodeByPage(MenuFunction menu,String pageUrl) {
		//F:\GitHub\jmax4SmartHome
		try {
//			/F:/development/one/tomcat7/wtpwebapps/jmax4SmartHome/WEB-INF/classes/
			String classPath = getClass().getResource("/").getPath();
			String projectUrl = classPath.replace("WEB-INF/classes/", "");
//			String projectUrl = System.getProperty("user.dir");
//			File directory = new File("");//设定为当前文件夹
//			System.out.println(directory.getAbsolutePath());
			StringBuffer result  = new StringBuffer("");
			BufferedReader br = new BufferedReader(new FileReader(projectUrl+"webpages/"+pageUrl));// 构造一个BufferedReader类来读取文件
			String s = null;
			while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
				result.append("\n" + s);
			}
			br.close();
			
			
			Pattern p = Pattern.compile("showName=\"(.+?)\" shiro=\"(.+?)\"");//shiro="xxxx"
			Matcher m = p.matcher(result);// 开始编译
			while (m.find()) {
				Operation operation = new Operation(new String(m.group(1).getBytes(), "UTF-8"), new String(m.group(2).getBytes(), "UTF-8"), menu);
				baseService.save(operation);
			}
		} catch (Exception e) {
			log.error("init button shiro code error",e);
		}
	}
}
